/*
 *  show linux API: settimer()
 *  设置一个定时器
 *  int settimer(int which, const struct itimerval *new_value, struct itimerrval *old_value)
 *      which:
 *              ITIMER_REAL :创建以真实时间倒计时的定时器，到期后会产生SIGALARM
 *              ITIMER_VIRTUAL:创建以进程虚拟时间倒计时的定时器,产生SIGVTALRM
 *              ITIMER_PROF: 创建一个profiling定时器，以进程时间倒计时，到时产生SIGPROF信号 
 *      new_value:
 *              struct itimerval{
 *                  struct timeval it_interval;//第一次到期后以后定时器的间隔时间
 *                  struct timerval it_value;  //第一次定时器的到期时间      
 *                  }
 *                  struct timerval{
 *                        timer_t tv_sec;
 *                        suseconds_t tv_usec;
 *                  }
 *      old_value:返回定时器的上一次设置
 * return: 0:success, -1 error
 *
 * 获取定时器当前的情况 
 *   int gettitimer(int which, struct itimerval *cuur_vlaue)
 */

//每个1s产生一行标准输出
#include <sys/time.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>

static int count = 0;

void printMes(int signo)
{
    printf("get a sigALRM, %d counts\n", ++count);
}

int main()
{
    int res = 0;
    struct itimerval tick;
    signal(SIGALRM, printMes);

    memset(&tick, 0, sizeof(tick));
    tick.it_value.tv_sec = 1;
    tick.it_value.tv_usec = 0;

    tick.it_interval.tv_sec = 1;
    tick.it_interval.tv_usec = 0;

    if (0 != setitimer(ITIMER_REAL, &tick, NULL))
    {
        printf("err set timer");
        exit(1);
    }

    while (1)
    {
        pause();
    }
    return 0;
}